﻿Imports DARSClassLibrary


Public Class Welcome
    Inherits System.Web.UI.Page
    Private uname As String
    Private pw As String
    Private ds As DataSet
    Private ds1 As DataSet
    Private CurrentUserType As String
    Private CurrentAdvisorID As String
    Private objStudent As Student
    Private objAdvisor As Advisor
    Private CurrentUser As User


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            btnSubmit.Attributes.Add("onclick", "return validateLogin();")
            txtPassword.Attributes.Add("type", "password")

        End If

    End Sub

    '''<summary>
    ''' This method does the following:
    ''' -Verify the login credentials
    ''' -If no user found, it will display a message on the login page
    ''' -If a user is found, it will create a session variable "CurrentUser" with all information of that user 
    ''' -Redirects the successfully logged in user to the correct menu page depending on the user type (student or advisor)
    ''' </summary>

    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        Dim ws = New DO_NOT_MOVE_ME_FROM_THIS_PROJECT.DARSWebservice

        uname = txtUsername.Text.Trim
        pw = txtPassword.Text.Trim
        Try
            ds = ws.getUserType(uname, pw)
            If ds.Tables(0).Rows.Count = 0 Then
                lblErrorMessage.Text = "Invalid user name or password. Please try again"
                txtPassword.Focus()
                txtPassword.Attributes.Add("onfocus", "document.getElementById('txtPassword').select();")
                Exit Sub
            Else
                CurrentUser = New User
                CurrentUser.TypeID = CInt(ds.Tables(0).Rows(0)("TypeID"))

                CurrentUserType = CStr(ds.Tables(0).Rows(0)("TypeName"))

                'This session variable is to use if we need to display the type of the logged in user on some other page
                'maybe useful if a user is an advisor and a student at the same time
                Session.Add("CurrentuserType", CurrentUserType)
                ds = ws.getUserPersonalInfo(uname, CurrentUser.TypeID)
                CurrentUser.UserID = uname
                'CurrentAdvisorID = CStr(ds1.Tables(0).Rows(0)("jjj"))
                ' Session.Add("CurrentAdvisorID", uname)
                CurrentUser.TypeID = CurrentUser.TypeID
                CurrentUser.FirstName = CStr(ds.Tables(0).Rows(0)("FirstName"))
                CurrentUser.LastName = CStr(ds.Tables(0).Rows(0)("LastName"))
                CurrentUser.Email = CStr(ds.Tables(0).Rows(0)("Email"))
                CurrentUser.StreetAddress = CStr(ds.Tables(0).Rows(0)("StreetAddress"))
                CurrentUser.City = CStr(ds.Tables(0).Rows(0)("City"))
                CurrentUser.State = CStr(ds.Tables(0).Rows(0)("State"))
                CurrentUser.ZipCode = CInt(ds.Tables(0).Rows(0)("ZipCode"))

                'NOT SURE IF THIS IS THE BEST WAY TO CREATE CURRENT USER OBJECT
                'BUT THE WAY THE DB IS SET UP I CAN'T THINK OF A MORE GENERAL WAY (if a new type of user is added, this code will need to change)
                If CurrentUser.TypeID = 1 Then

                    Dim StudentID As String = CStr(ds.Tables(0).Rows(0)("StudentID"))
                    Dim StudentTypeID As String = CStr(ds.Tables(0).Rows(0)("StudentTypeID"))
                    objStudent = New Student(StudentID, StudentTypeID, CurrentUser)
                    objStudent.FirstName = CurrentUser.FirstName
                    objStudent.LastName = CurrentUser.LastName

                    Session.Add("CurrentUser", objStudent)
                    Response.Redirect("LandingPage.aspx")
                ElseIf CurrentUser.TypeID = 2 Then

                    Dim AdvisorID As String = CStr(ds.Tables(0).Rows(0)("AdvisorID"))
                    Dim AdvisorDepartmentID As String = CStr(ds.Tables(0).Rows(0)("DepartmentID"))
                    objAdvisor = New Advisor(AdvisorID, AdvisorDepartmentID, CurrentUser)
                    objAdvisor.FirstName = CurrentUser.FirstName
                    objAdvisor.LastName = CurrentUser.LastName

                    Session.Add("CurrentUser", objAdvisor)
                    Response.Redirect("AdvisorLandingPage.aspx")
                End If
            End If

        Catch ex As Exception
            lblErrorMessage.Text = "A connection error Occured. Try again : "
        End Try

    End Sub



End Class